package com.rc.saas.mini.controller.tenant;

import com.rc.saas.base.MinipBaseController;
import com.rc.saas.mini.common.base.DataTResponse;
import com.rc.saas.mini.common.constant.SessionConstants;
import com.rc.saas.mini.model.rc.RcSetting;
import com.rc.saas.mini.model.rc.RcSettingExample;
import com.rc.saas.mini.model.tenant.Tenant;
import com.rc.saas.mini.model.tenant.TenantSetting;
import com.rc.saas.mini.service.member.MemberInfoService;
import com.rc.saas.mini.service.rc.RcSettingService;
import com.rc.saas.mini.service.tenant.TenantCooperationService;
import com.rc.saas.mini.service.tenant.TenantPaymentActivityService;
import com.rc.saas.mini.service.tenant.TenantService;
import com.rc.saas.mini.service.tenant.TenantSettingService;
import com.rc.saas.mini.vo.rc.RcSettingVo;
import com.rc.saas.mini.vo.tenant.TenantVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangzhengwei
 * @create 2021/12/16
 */
@Controller
@RequestMapping("/tenant")
@Api(tags = "19.运营商相关")
public class TenantController extends MinipBaseController {
    private static Logger logger = LogManager.getLogger(TenantController.class);

    @Autowired
    private MemberInfoService memberInfoService;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private TenantPaymentActivityService tenantPaymentActivityService;

    @Autowired
    private TenantSettingService tenantSettingService;
    @Autowired
    private RcSettingService rcSettingService;

    @Autowired
    private TenantCooperationService tenantCooperationService;

    /**
     * 获取运营商设置
     */
    @ResponseBody
    @RequestMapping("/open/selectTenantSetting")
    @ApiOperation(value = "（弃用）获取运营商设置(如：客服电话，其他等等)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public DataTResponse<RcSettingVo> selectTenantSetting(HttpServletRequest request) {
        String tenantCode = (String) request.getSession().getAttribute(SessionConstants.SESSION_TENANT);
        if (tenantCode == null || StringUtils.isBlank(tenantCode)) {
            logger.error("找不到对应运营商信息:{}", tenantCode);
            return DataTResponse.FAIL("找不到对应运营商信息");
        }
        Tenant tenant = tenantService.selectByTenantCode(tenantCode);
        Map<String, Object> map = new HashMap<>();
        map.put("servicePhone", tenant.getServicePhone());

        RcSetting rcSetting = rcSettingService.selectByExample(new RcSettingExample()).stream().findFirst().orElseGet(()->null);
        RcSettingVo rcSettingVo=new RcSettingVo();
        BeanUtils.copyProperties(rcSetting,rcSettingVo);
        rcSettingVo.setServicePhone(tenant.getServicePhone());
        return DataTResponse.SUCCESS_DATA(rcSettingVo);
    }

    /**
     * 获取运营商设置
     */
    @ResponseBody
    @RequestMapping("/open/selectTenant")
    @ApiOperation(value = "获取运营商设置(如：客服电话，在线客服其他等等)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public DataTResponse<TenantVo> selectTenant(HttpServletRequest request) {
        String tenantCode = (String) request.getSession().getAttribute(SessionConstants.SESSION_TENANT);
        if (tenantCode == null || StringUtils.isBlank(tenantCode)) {
            logger.error("找不到对应运营商信息:{}", tenantCode);
            return DataTResponse.FAIL("找不到对应运营商信息");
        }
        Tenant tenant = tenantService.selectByTenantCode(tenantCode);

        TenantVo tenantVo=new TenantVo();
        BeanUtils.copyProperties(tenant,tenantVo);
        return DataTResponse.SUCCESS_DATA(tenantVo);
    }

}
